// 分类数据相关的store
import category from '@/api/category'
const state = {
  getCategoryList1Data:[],
  getCategoryList2Data:[],
  getCategoryList3Data:[]
}

const mutations = {
  GET_CATEGORY_LIST1(state,val){
    state.getCategoryList1Data=val.map((item)=>{
      return {
        ...item,
        level:1,
        hasChildren:true
      }
    })
  },
  GET_CATEGORY_LIST2(state,val){
    state.getCategoryList2Data=val.map((item)=>{
      return {
        ...item,
        hasChildren:true
      }
    })
  },
  GET_CATEGORY_LIST3(state,val){
    state.getCategoryList3Data=val
  },
  RESTE_LIST2(){
    this.getCategoryId2 = []
  },
  RESTE_LIST3(){
    this.getCategoryId3 = []
  }
}

const actions = {
  async getCategoryList1({commit}){
    const res = await category.getCategoryId1()
    if(res.code===200){
      commit('GET_CATEGORY_LIST1',res.data)
    }
  },
  async getCategoryList2({commit},id){
    const res = await category.getCategoryId2(id)
    commit('RESTE_LIST2')  //清空2
    commit('RESTE_LIST3')  //清空3
    if(res.code===200){
      commit('GET_CATEGORY_LIST2',res.data)
    }
  },
  async getCategoryList3({commit},id){
    const res = await category.getCategoryId3(id)
    commit('RESTE_LIST3')  //清空3
    // console.log(res,'89') 
    if(res.code===200){
      commit('GET_CATEGORY_LIST3',res.data)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

